package com.hippo.core.state

import androidx.annotation.Keep
import com.google.gson.Gson
import com.hippo.core.R
import com.hippo.core.config.AppConsts
import com.hippo.core.tuya.DpValue
import com.hippo.core.tuya.toSpeedy
import com.thingclips.smart.sdk.bean.DeviceBean

@Keep
data class ControlState(
    val isLoading:Boolean = false,
    val devId:String? = null,
    val msg:String? = null,
    val speedy:Int = 0,
    val controlType:Int = AppConsts.ControlType.NONE,
    val wifiSignal:Int = 0,
    val device: DeviceBean? = null,
    val dps:Map<String,Any> = emptyMap(),
    val deviceValue:DeviceValue = DeviceValue(),
)

@Keep
data class DeviceValue(
    val switch:Boolean? = false,
    val switch_go:Boolean?= false,
    val mode:String? = null,
    val direction_control:String? = null,
    val status:String? = null,
    val battery_percentage:Int? = null,
    val suction:String? = null,
    val clean_area:Int? = null,
    val clean_time:Int? = null,
//    val fault:String? = null,

    val water_quality_ph:Int? = null,
    val water_quality_salt:Int? = null,
    val water_quality_oxygen:Int? = null,
    val water_quality_temperature:Int? = null,
    val water_quality_ec:Int? = null,
    val sensor_life:Int? = null,
    val slide_life:Int? = null,
    val appointment_notice:Boolean? = null,
)


fun DeviceValue.fromJson(json:String):DeviceValue{
    return Gson().fromJson(json, DeviceValue::class.java)
}

fun ControlState.copyValue(value: DeviceValue):ControlState{
    val deviceValue = DeviceValue(
        switch = this.deviceValue?.switch ?: false,
        switch_go = this.deviceValue?.switch_go ?: false,
        mode = this.deviceValue?.mode,
        direction_control = this.deviceValue?.direction_control,
        status = this.deviceValue?.status,
        battery_percentage = this.deviceValue?.battery_percentage,
        suction = this.deviceValue?.suction,
        clean_area = this.deviceValue?.clean_area,
        clean_time = this.deviceValue?.clean_time,
    )
    return this.copy(deviceValue = deviceValue)
}


fun DeviceValue.toWorkStatusIcon(): Int?{
    val speedy = suction.toSpeedy()
    when(status){
        DpValue.Status.smart_clean,DpValue.Status.spot_clean->{
            return if(speedy == 1 || speedy == 2){
                return R.mipmap.time_n
            }else{
                return R.mipmap.efficient_n
            }
        }
        DpValue.Status.cleaning->{
            return R.mipmap.manual
        }
        DpValue.Status.standby->{
            return R.mipmap.standby
        }
        else->{
            return null
        }
    }
}




